package com.second.app.proxy;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @Author GYZ
 * @Date 2023/9/18
 * <pre>
 *
 * </pre>
 */
public class JdkDynamicProxy implements InvocationHandler {


    private Object target;


    public JdkDynamicProxy(Object target) {
        this.target = target;
    }


    public Object getProxy(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("【记录日志】准备执行方法："+method.getName()+"，参数列表："+ Arrays.toString(args));

        Object result = method.invoke(target, args);

        System.out.println("【记录日志】方法执行结束："+method.getName()+"，返回值为："+ result);

        return result;
    }
}
